import { _decorator, Component, EventTouch, Input, Node, Vec3 } from 'cc';
import { SinglePallet } from './SinglePallet';
import { ClearLoad } from './ClearLoad';
const { ccclass, property } = _decorator;

@ccclass('TouchPallet')
/**
 * 移动托盘
 */
export class TouchPallet extends SinglePallet {

    //消失层
    @property(Node)
    private ClearLoad: Node = null;

    //是否被点击了
    private IsTouch: boolean = false;

    protected start(): void {
        this.Pallet.on(Input.EventType.TOUCH_START, this.TouchCb, this);
        this.Pallet.on(Input.EventType.TOUCH_MOVE, this.TouchCb, this);
        this.Pallet.on(Input.EventType.TOUCH_END, this.TouchCb, this)
    }

    /**
     * 手指操作回调
     */
    private TouchCb(_e: EventTouch) {
        if (_e.type == Input.EventType.TOUCH_START) {
            this.IsTouch = true;
        } else if (_e.type == Input.EventType.TOUCH_MOVE) {
            if (this.IsTouch) {
                this.Pallet.setWorldPosition(new Vec3(_e.getUILocation().x, _e.getUILocation().y))
            }
        } else if (_e.type == Input.EventType.TOUCH_END) {
            this.IsTouch = false;
            if (this.ClearLoad.getComponent(ClearLoad).CheckHit(this.Pallet)) {
                this.IamHide();
            }
            this.BackToInitPos();

        }
    }

    /**
     * 回到初始位置
     */
    public BackToInitPos() {
        this.Pallet.setPosition(new Vec3(0, 0, 0));
    }


}


